package com.plugin.sproutcore.util;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;

import com.plugin.sproutcore.Activator;

public class ProjectUtil 
{
	public static IProject getProjectFromSelection(ISelection selection)
	{
		if (selection != null && selection.isEmpty() == false
				&& selection instanceof IStructuredSelection) 
		{
			IStructuredSelection ssel = (IStructuredSelection) selection;
			Object element = ssel.getFirstElement();
			if (element instanceof IResource)
			{
				IResource resource = ((IResource)element);
				return resource.getProject();
			}
		}
		return null;
	}
	
	public static IStatus createWarningStatus(String message) 
	{
		return createWarningStatus(message, null);
	}
	public static IStatus createWarningStatus(String message, Throwable exception) 
	{
		return new Status(IStatus.WARNING, Activator.PLUGIN_ID, IStatus.OK, 
				message, exception);
	}
	
	public static IStatus createErrorStatus(String message) 
	{
		return createErrorStatus(message, null);
	}
	public static IStatus createErrorStatus(String message, Throwable exception) 
	{
		return new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.OK, 
				message, exception);
	}
	
	public static String getRelativePath(String parentPath, String filePath)
	{
		return filePath.replace(parentPath, "");
	}

}
